package com.phkj.qdh.ui.fragment.instructionsheet

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.hjq.toast.ToastUtils
import com.phkj.qdh.base.BaseVM
import com.phkj.qdh.bean.InstructionBean
import com.phkj.qdh.bean.InstructionDetailBean
import com.phkj.qdh.bean.JingQingDetailBean
import com.phkj.qdh.bean.JingQingResult
import com.phkj.qdh.bean.LawOrTeamInspectBean
import com.phkj.qdh.net.code
import com.phkj.qdh.net.msg
import com.phkj.qdh.repository.InstructionRepository
import com.phkj.qdh.utils.requestIO
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.onCompletion
import java.util.HashMap

class InstructionVM(private val instruct: InstructionRepository) : BaseVM() {
    private val _instructionList = MutableSharedFlow<ArrayList<InstructionBean>>()
    val instructionList get() = _instructionList

    private val _state = MutableSharedFlow<Int>()
    val state get() = _state
    fun getInstructionList(map: HashMap<String, Any?>) {
        requestIO {
            instruct.getInstructionList(map).catch { cause ->
                ToastUtils.show(cause.msg)
                stopLoading()
            }.collectLatest {
                _instructionList.emit(it.data)
            }
        }
    }


    private val _instructionListNo = MutableSharedFlow<ArrayList<InstructionBean>>()
    val instructionListNo get() = _instructionListNo
    fun getInstructionListNo(map: HashMap<String, Any?>) {
        requestIO {
            instruct.getInstructionList(map).catch { cause ->
                ToastUtils.show(cause.msg)
                stop(1)
            }.collectLatest {
                _instructionListNo.emit(it.data)
                }
        }
    }



    private val _instructionDetail = MutableSharedFlow<InstructionDetailBean>()
    val instructionDetail get() = _instructionDetail
    fun getInstructionDetail(map: HashMap<String, Any?>) {
        requestIO {
            instruct.getInstructionDetail(map).catch { cause ->
                ToastUtils.show(cause.msg)
                stop(1)
            }.collectLatest {
                _instructionDetail.emit(it)
            }
        }
    }


    private val _instructionRejectResult = MutableSharedFlow<Boolean>()
    val instructionRejectResult get() = _instructionRejectResult
    fun getInstructionReject(id:Int) {
        requestIO {
            instruct.getInstructionReject(id).catch { cause ->
                ToastUtils.show(cause.msg)
               stop(1)
            }.collectLatest {
                _instructionRejectResult.emit(it)
            }
        }
    }


    private val _instructionReHandleResult = MutableSharedFlow<Boolean>()
    val instructionReHandleResult get() = _instructionReHandleResult
    fun saveInstructionReject(map : HashMap<String, Any?>) {
        requestIO {
            instruct.getInstructionFeedback(map).catch { cause ->
                ToastUtils.show(cause.msg)
                stop(1)
            }.collectLatest {
                _instructionReHandleResult.emit(it)
            }
        }
    }


    private val _jingqingDetail = MutableSharedFlow<ArrayList<JingQingDetailBean>>()
    val jingqingDetail get() = _jingqingDetail
    fun getJingQingDeatil(id:String) {
        requestIO {
            instruct.getJingQingDeatil(id).catch { cause ->
                ToastUtils.show(cause.msg)
                stop(1)
            }.collectLatest {
                _jingqingDetail.emit(it.list)
            }
        }
    }
    suspend fun stop(i:Int){
        _state.emit(i)
    }
}

fun getInstructionVMProvider() = InstructionVMProvider(InstructionRepository)

class InstructionVMProvider(val instruct: InstructionRepository) : ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        return InstructionVM(instruct) as T
    }
}